#!/bin/bash
# 测试环境：Ubuntu-20.04

echo "打印内存相关信息"
memory_total=`free -h |grep -i mem |awk -F : '{print $2}' |awk '{print $1}'`
echo "memory total: $memory_total"
memory_total1=`free  |grep -i mem |awk -F : '{print $2}' |awk '{print $1}'`
memory_used=`free  |grep -i mem |awk -F : '{print $2}' |awk '{print $2}'`
memory_usage=`echo "scale=2; ($memory_used / $memory_total1)*100" | bc `
echo "memory usage: $memory_usage"%""

swap_total=`free -h |grep -i swap |awk -F : '{print $2}' |awk '{print $1}'`
echo "swap total: $swap_total"
swap_total1=`free  |grep -i swap |awk -F : '{print $2}' |awk '{print $1}'`
swap_used=`free  |grep -i swap |awk -F : '{print $2}' |awk '{print $2}'`
swap_usage=`echo "scale=2; ($swap_used / $swap_total1)*100" | bc `
echo "swap usage: $swap_usage"%""

echo "打印CPU相关信息"
cpu_model=`cat /proc/cpuinfo  |grep 'model name' |awk -F : '{print $2}'`
echo "cpu model: $cpu_model "
cpu_cores=`cat /proc/cpuinfo |grep 'cpu cores'|awk -F : '{print $2}'`
echo "cpu cores: $cpu_cores * core(s) "
cpu_usage=`top -n 1 |grep '%Cpu'|awk -F : '{print $2}'|awk  -F , '{print $1}'|sed 's/us//'`
echo "cpu usage: $cpu_usage"%""

# Ubuntu服务器上监控内存、磁盘和cpu使用率shell脚本 https://blog.csdn.net/weixin_44861214/article/details/114532801
# 使用bash shell 查看Linux系统的CPU和内存  https://blog.51cto.com/u_14101497/2404396
# ./memory_cpu.sh

# 打印内存相关信息
# memory total: 3.8Gi
# memory usage: 40.00%
# swap total: 923Mi
# swap usage: 0%
# 打印CPU相关信息
# cpu model:  Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
#  Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz 
# cpu cores:  1
#  1 * core(s) 
# cpu usage:   2.9 %